package io.renren.modules.lables.service;

import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.meta.service.EventEntityService;
import io.renren.modules.meta.service.SubjectService;
import io.renren.modules.tag.utils.SnowFlake;
import io.renren.modules.tag.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

import static io.renren.modules.tag.utils.SqlUtils.generateQuery;

@Service
@Slf4j
@DataSource("slave_3")
public class LabelService {
    @Autowired
    SnowFlake snowFlake;
    @Autowired
    EventEntityService eventService;
    @Autowired
    SubjectService subjectService;
    public HashMap<String, List<String>> circleSelect(String[] tags) {
        String sql = generateQuery(tags);
        List<Map<String, Object>> stringObjectMap = SqlRunner.db().selectList(sql);
        HashMap<String, List<String>> stringListHashMap = new HashMap<>();
        for (Map<String, Object> objectMap : stringObjectMap) {
            Set<String> keys = objectMap.keySet();
            String subject = null;
            String value;
            List<String> list = new ArrayList<>();
            for (String key : keys) {
                if (key.equals("subject")) {
                    subject = (String) objectMap.get(key);
                } else {
                    value = (String) objectMap.get(key);
                    String[] split = value.split(",");
                    list.addAll(Arrays.asList(split));
                }
            }
            stringListHashMap.put(subject, list);
        }
        return stringListHashMap;
    }
    public void saveBatch(LocalDateTime now,String subjectName) {
        String query = "INSERT INTO `labels`.`label_tag_bitmap` " +
                "SELECT tagname, tagvalue, inserttime, subject, userid/(10000000000/3), to_bitmap(userid) " +
                "FROM user_tag_xxx " +
                "WHERE inserttime={0} AND subject={1}";
        log.debug(query);
        SqlRunner.db().insert(query, now.toString(), subjectName);
    }

    public void saveBatch() {
        String query = "INSERT INTO `labels`.`label_tag_bitmap` " +
                "SELECT tagname, tagvalue, inserttime, subject, userid/(10000000000/3), to_bitmap(userid) " +
                "FROM user_tag_xxx ";
        log.debug(query);
        SqlRunner.db().insert(query);
    }
//
//    public List<EventEntity> getEvents(String subject, Long userid) {
//        QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("subject_name",subject);
//        SubjectEntity subjectEntity = subjectService.getOne(queryWrapper);
//        String subjectTable = subjectEntity.getSubjectTable();
//        QueryWrapper<EventEntity> query = new QueryWrapper<>();
//        query.eq("subject_id", userid).eq("subject_table",subjectTable).orderByAsc("event_time");
//        List<EventEntity> list = eventService.list(query);
//        return list;
//    }

    public ArrayList<String> getSubjectTags(String subject, Long userid) {
        String sql = SqlUtils.getSubjectTagSql(subject, userid);
        List<Map<String, Object>> list = SqlRunner.db().selectList(sql);
        Set<String> results = new HashSet<>();
        for (Map<String, Object> stringObjectMap : list) {
            Object tagvalue = stringObjectMap.get("tagvalue");
            results.add((String) tagvalue);
        }
        return new ArrayList<>(results);
    }


}
